<html><head><title>/Users/Alexander/SandBox/nwscode/nwscode/ugc.py</title>
        <script type="text/javascript"><!--
        
function show_line_range() {
    var href = document.location.href;
    if (href.indexOf('?') == -1) {
        return;
    }
    var qs = href.substring(href.indexOf('?')+1);
    if (qs.indexOf('#') >= 0) {
        qs = qs.substring(0, qs.indexOf('#'));
    }
    var first = qs.match(/f=(\d+)/)[1];
    var last = qs.match(/l=(\d+)/)[1];
    if (! first || ! last) {
        return;
    }
    var anchors = document.getElementsByTagName('A');
    var container = document.createElement('DIV');
    container.className = 'highlighted';
    var children = [];
    var start = null;
    var parent = null;
    var highlight = false;
    for (var i = 0; i < anchors.length; i++) {
        var el = anchors[i];
        if (el.getAttribute('name') == first) {
            start = el.previousSibling;
            parent = el.parentNode;
            highlight = true;
        }
        if (el.getAttribute('name') == last) {
            break;
        }
        if (highlight) {
            children[children.length] = el;
            el = el.nextSibling;
            while (el && el.tagName != 'A') {
                children[children.length] = el;
                el = el.nextSibling;
            }
        }
    }
    for (i=0; i<children.length; i++) {
        container.appendChild(children[i]);
    }
    if (start) {
        start.parentNode.insertBefore(container, start.nextSibling);
    } else {
        parent.insertBefore(container, parent.childNodes[0]);
    }
}

        // --></script>
        <style>
        div.python {
          color: #333
        }
        div.python a.lnum {
          color: #555;
          background-color: #eee;
          border-right: 1px solid #999;
          padding-right: 2px;
          margin-right: 4px;
        }
        div.python span.comment { color: #933 }
        div.python span.keyword { color: #a3e; font-weight: bold  }
        div.python span.op { color: #c96 }
        div.python span.string { color: #6a6 }
        div.python span.name { }
        div.python span.text { color: #333 }
        div.highlighted { background-color: #ff9; border: 1px solid #009 }
        </style></head><body onload="show_line_range()"><div class="python"><code><a class="lnum" href="#1" name="1">0001</a><span class="comment">#!/usr/bin/env&#0160;python</span><br />
<a class="lnum" href="#2" name="2">0002</a><span class="comment"></span><span class="comment">#&#0160;encoding:&#0160;utf-8</span><br />
<a class="lnum" href="#3" name="3">0003</a><span class="comment"></span><span class="string">"""</span><br />
<a class="lnum" href="#4" name="4">0004</a><span class="string">A&#0160;parser&#0160;for&#0160;the&#0160;*Universal&#0160;Geographic&#0160;Code*.</span><br />
<a class="lnum" href="#5" name="5">0005</a><span class="string"></span><br />
<a class="lnum" href="#6" name="6">0006</a><span class="string">Created&#0160;by&#0160;Alexander&#0160;Ross&#0160;on&#0160;2006-07-20.</span><br />
<a class="lnum" href="#7" name="7">0007</a><span class="string">Copyright&#0160;(c)&#0160;2006&#0160;NOAA's&#0160;National&#0160;Weather&#0160;Service.&#0160;All&#0160;rights&#0160;reserved.</span><br />
<a class="lnum" href="#8" name="8">0008</a><span class="string">"""</span><br />
<a class="lnum" href="#9" name="9">0009</a><br />
<a class="lnum" href="#10" name="10">0010</a><span class="name">__all__</span>&#0160;<span class="op">=</span>&#0160;<span class="op">[</span><span class="string">"UgcError"</span><span class="op">,</span>&#0160;<span class="string">"Ugc"</span><span class="op">]</span><br />
<a class="lnum" href="#11" name="11">0011</a><br />
<a class="lnum" href="#12" name="12">0012</a><span class="keyword">import</span>&#0160;<span class="name">re</span><br />
<a class="lnum" href="#13" name="13">0013</a><span class="keyword">from</span>&#0160;<span class="name">nwscode</span>&#0160;<span class="keyword">import</span>&#0160;<span class="name">NwsCode</span><span class="op">,</span>&#0160;<span class="name">NwsCodeError</span><br />
<a class="lnum" href="#14" name="14">0014</a><span class="keyword">from</span>&#0160;<span class="name">misc</span>&#0160;<span class="keyword">import</span>&#0160;<span class="name">Bunch</span><span class="op">,</span>&#0160;<span class="name">RelativeTime</span><br />
<a class="lnum" href="#15" name="15">0015</a><br />
<a class="lnum" href="#16" name="16">0016</a><span class="name">IDENT</span>&#0160;<span class="op">=</span>&#0160;<span class="string">r"[A-Z]{3}"</span><br />
<a class="lnum" href="#17" name="17">0017</a><span class="name">NUMBER</span>&#0160;<span class="op">=</span>&#0160;<span class="string">r"[0-9]{3}"</span><br />
<a class="lnum" href="#18" name="18">0018</a><span class="name">DELIM</span>&#0160;<span class="op">=</span>&#0160;<span class="string">r"&gt;|\-\n?"</span><br />
<a class="lnum" href="#19" name="19">0019</a><span class="name">AREA</span>&#0160;<span class="op">=</span>&#0160;<span class="string">r"%s(?:%s(?:%s))+"</span>&#0160;<span class="op">%</span>&#0160;<span class="op">(</span><span class="name">IDENT</span><span class="op">,</span>&#0160;<span class="name">NUMBER</span><span class="op">,</span>&#0160;<span class="name">DELIM</span><span class="op">)</span><br />
<a class="lnum" href="#20" name="20">0020</a><span class="name">TIME</span>&#0160;<span class="op">=</span>&#0160;<span class="string">r"[0-9]{6}"</span><br />
<a class="lnum" href="#21" name="21">0021</a><span class="name">UGC</span>&#0160;<span class="op">=</span>&#0160;<span class="string">r"^((?:%s)+)(%s)[\-]$"</span>&#0160;<span class="op">%</span>&#0160;<span class="op">(</span><span class="name">AREA</span><span class="op">,</span>&#0160;<span class="name">TIME</span><span class="op">)</span><br />
<a class="lnum" href="#22" name="22">0022</a><br />
<a class="lnum" href="#23" name="23">0023</a><span class="keyword">class</span>&#0160;<span class="name">UgcError</span><span class="op">(</span><span class="name">NwsCodeError</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#24" name="24">0024</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">pass</span><br />
<a class="lnum" href="#25" name="25">0025</a><br />
<a class="lnum" href="#26" name="26">0026</a><span class="keyword">class</span>&#0160;<span class="name">Ugc</span><span class="op">(</span><span class="name">NwsCode</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#27" name="27">0027</a>&#0160;&#0160;&#0160;&#0160;<span class="string">"""</span><br />
<a class="lnum" href="#28" name="28">0028</a><span class="string">&#0160;&#0160;&#0160;&#0160;A&#0160;*Universal&#0160;Geographic&#0160;Code*&#0160;parser,&#0160;inherits&#0160;from&#0160;`NwsCode`.</span><br />
<a class="lnum" href="#29" name="29">0029</a><span class="string">&#0160;&#0160;&#0160;&#0160;</span><br />
<a class="lnum" href="#30" name="30">0030</a><span class="string">&#0160;&#0160;&#0160;&#0160;For&#0160;a&#0160;full&#0160;description&#0160;of&#0160;the&#0160;UGC&#0160;format&#0160;see&#0160;the&#0160;National&#0160;Weather</span><br />
<a class="lnum" href="#31" name="31">0031</a><span class="string">&#0160;&#0160;&#0160;&#0160;Service's&#0160;UGC&#0160;Directive:</span><br />
<a class="lnum" href="#32" name="32">0032</a><span class="string">&#0160;&#0160;&#0160;&#0160;</span><br />
<a class="lnum" href="#33" name="33">0033</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;http://www.nws.noaa.gov/directives/sym/pd01017002curr.pdf</span><br />
<a class="lnum" href="#34" name="34">0034</a><span class="string">&#0160;&#0160;&#0160;&#0160;</span><br />
<a class="lnum" href="#35" name="35">0035</a><span class="string">&#0160;&#0160;&#0160;&#0160;Attributes:</span><br />
<a class="lnum" href="#36" name="36">0036</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;</span><br />
<a class="lnum" href="#37" name="37">0037</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;``raw``</span><br />
<a class="lnum" href="#38" name="38">0038</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;The&#0160;raw&#0160;UGC&#0160;string,&#0160;undecoded.</span><br />
<a class="lnum" href="#39" name="39">0039</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;</span><br />
<a class="lnum" href="#40" name="40">0040</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;``code``</span><br />
<a class="lnum" href="#41" name="41">0041</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;Provides&#0160;access&#0160;to&#0160;the&#0160;individual&#0160;code&#0160;groups.&#0160;&#0160;In&#0160;this&#0160;case,</span><br />
<a class="lnum" href="#42" name="42">0042</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;``area``&#0160;and&#0160;``expiration``.</span><br />
<a class="lnum" href="#43" name="43">0043</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;</span><br />
<a class="lnum" href="#44" name="44">0044</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;``areas``</span><br />
<a class="lnum" href="#45" name="45">0045</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;A&#0160;list&#0160;of&#0160;affected&#0160;areas.</span><br />
<a class="lnum" href="#46" name="46">0046</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;</span><br />
<a class="lnum" href="#47" name="47">0047</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;``expiration``</span><br />
<a class="lnum" href="#48" name="48">0048</a><span class="string">&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;The&#0160;product&#0160;expiration&#0160;time.</span><br />
<a class="lnum" href="#49" name="49">0049</a><span class="string">&#0160;&#0160;&#0160;&#0160;"""</span><br />
<a class="lnum" href="#50" name="50">0050</a><br />
<a class="lnum" href="#51" name="51">0051</a>&#0160;&#0160;&#0160;&#0160;<span class="name">pattern</span>&#0160;<span class="op">=</span>&#0160;<span class="name">re</span><span class="op">.</span><span class="name">compile</span><span class="op">(</span><span class="name">UGC</span><span class="op">,</span>&#0160;<span class="name">re</span><span class="op">.</span><span class="name">M</span><span class="op">)</span><br />
<a class="lnum" href="#52" name="52">0052</a>&#0160;&#0160;&#0160;&#0160;<span class="name">error</span>&#0160;<span class="op">=</span>&#0160;<span class="name">UgcError</span><br />
<a class="lnum" href="#53" name="53">0053</a><br />
<a class="lnum" href="#54" name="54">0054</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">def</span>&#0160;<span class="name">_process_matches</span><span class="op">(</span><span class="name">self</span><span class="op">,</span>&#0160;<span class="name">matches</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#55" name="55">0055</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">self</span><span class="op">.</span><span class="name">areas</span>&#0160;<span class="op">=</span>&#0160;<span class="name">self</span><span class="op">.</span><span class="name">_expand_area</span><span class="op">(</span><span class="name">matches</span><span class="op">[</span><span class="number">0</span><span class="op">]</span><span class="op">)</span><br />
<a class="lnum" href="#56" name="56">0056</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">time_string</span>&#0160;<span class="op">=</span>&#0160;<span class="name">matches</span><span class="op">[</span><span class="number">1</span><span class="op">]</span><br />
<a class="lnum" href="#57" name="57">0057</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">day</span><span class="op">,</span>&#0160;<span class="name">hour</span><span class="op">,</span>&#0160;<span class="name">minute</span>&#0160;<span class="op">=</span>&#0160;<span class="name">time_string</span><span class="op">[</span><span class="op">:</span><span class="number">2</span><span class="op">]</span><span class="op">,</span>&#0160;<span class="name">time_string</span><span class="op">[</span><span class="number">2</span><span class="op">:</span><span class="number">4</span><span class="op">]</span><span class="op">,</span>&#0160;<span class="name">time_string</span><span class="op">[</span><span class="number">4</span><span class="op">:</span><span class="op">]</span><br />
<a class="lnum" href="#58" name="58">0058</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">self</span><span class="op">.</span><span class="name">expiration</span>&#0160;<span class="op">=</span>&#0160;<span class="name">RelativeTime</span><span class="op">(</span><span class="name">day</span><span class="op">,</span>&#0160;<span class="name">hour</span><span class="op">,</span>&#0160;<span class="name">minute</span><span class="op">)</span><br />
<a class="lnum" href="#59" name="59">0059</a><br />
<a class="lnum" href="#60" name="60">0060</a>&#0160;&#0160;&#0160;&#0160;<span class="keyword">def</span>&#0160;<span class="name">_expand_area</span><span class="op">(</span><span class="name">area_string</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#61" name="61">0061</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">areas</span>&#0160;<span class="op">=</span>&#0160;<span class="op">[</span><span class="op">]</span><br />
<a class="lnum" href="#62" name="62">0062</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">for</span>&#0160;<span class="name">m</span>&#0160;<span class="keyword">in</span>&#0160;<span class="name">re</span><span class="op">.</span><span class="name">compile</span><span class="op">(</span><span class="string">r"(%s)"</span>&#0160;<span class="op">%</span>&#0160;<span class="name">AREA</span><span class="op">,</span>&#0160;<span class="name">re</span><span class="op">.</span><span class="name">M</span><span class="op">)</span><span class="op">.</span><span class="name">finditer</span><span class="op">(</span><span class="name">area_string</span><span class="op">)</span><span class="op">:</span><br />
<a class="lnum" href="#63" name="63">0063</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">area</span>&#0160;<span class="op">=</span>&#0160;<span class="name">m</span><span class="op">.</span><span class="name">group</span><span class="op">(</span><span class="number">0</span><span class="op">)</span><span class="op">.</span><span class="name">replace</span><span class="op">(</span><span class="string">'\n'</span><span class="op">,</span>&#0160;<span class="string">''</span><span class="op">)</span><br />
<a class="lnum" href="#64" name="64">0064</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">ident</span><span class="op">,</span>&#0160;<span class="name">numbers</span>&#0160;<span class="op">=</span>&#0160;<span class="name">area</span><span class="op">[</span><span class="op">:</span><span class="number">3</span><span class="op">]</span><span class="op">,</span>&#0160;<span class="name">area</span><span class="op">[</span><span class="number">3</span><span class="op">:</span><span class="op">]</span><br />
<a class="lnum" href="#65" name="65">0065</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">numbers</span>&#0160;<span class="op">=</span>&#0160;<span class="name">numbers</span><span class="op">.</span><span class="name">strip</span><span class="op">(</span><span class="string">'-'</span><span class="op">)</span><span class="op">.</span><span class="name">split</span><span class="op">(</span><span class="string">'-'</span><span class="op">)</span><br />
<a class="lnum" href="#66" name="66">0066</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">buf</span>&#0160;<span class="op">=</span>&#0160;<span class="op">[</span><span class="op">]</span><br />
<a class="lnum" href="#67" name="67">0067</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">for</span>&#0160;<span class="name">num</span>&#0160;<span class="keyword">in</span>&#0160;<span class="name">numbers</span><span class="op">:</span><br />
<a class="lnum" href="#68" name="68">0068</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">if</span>&#0160;<span class="string">"&gt;"</span>&#0160;<span class="keyword">in</span>&#0160;<span class="name">num</span><span class="op">:</span><br />
<a class="lnum" href="#69" name="69">0069</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">first</span><span class="op">,</span>&#0160;<span class="name">last</span>&#0160;<span class="op">=</span>&#0160;<span class="name">num</span><span class="op">.</span><span class="name">split</span><span class="op">(</span><span class="string">"&gt;"</span><span class="op">)</span><br />
<a class="lnum" href="#70" name="70">0070</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">first</span><span class="op">,</span>&#0160;<span class="name">last</span>&#0160;<span class="op">=</span>&#0160;<span class="name">int</span><span class="op">(</span><span class="name">first</span><span class="op">)</span><span class="op">,</span>&#0160;<span class="name">int</span><span class="op">(</span><span class="name">last</span><span class="op">)</span><br />
<a class="lnum" href="#71" name="71">0071</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">buf</span><span class="op">.</span><span class="name">extend</span><span class="op">(</span><span class="op">[</span><span class="string">"%03i"</span>&#0160;<span class="op">%</span>&#0160;<span class="name">n</span>&#0160;<span class="keyword">for</span>&#0160;<span class="name">n</span>&#0160;<span class="keyword">in</span>&#0160;<span class="name">range</span><span class="op">(</span><span class="name">first</span><span class="op">,</span>&#0160;<span class="name">last</span>&#0160;<span class="op">+</span>&#0160;<span class="number">1</span><span class="op">)</span><span class="op">]</span><span class="op">)</span><br />
<a class="lnum" href="#72" name="72">0072</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">else</span><span class="op">:</span><br />
<a class="lnum" href="#73" name="73">0073</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">buf</span><span class="op">.</span><span class="name">append</span><span class="op">(</span><span class="name">num</span><span class="op">)</span><br />
<a class="lnum" href="#74" name="74">0074</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="name">areas</span><span class="op">.</span><span class="name">extend</span><span class="op">(</span><span class="op">[</span><span class="name">ident</span>&#0160;<span class="op">+</span>&#0160;<span class="name">num</span>&#0160;<span class="keyword">for</span>&#0160;<span class="name">num</span>&#0160;<span class="keyword">in</span>&#0160;<span class="name">buf</span><span class="op">]</span><span class="op">)</span><br />
<a class="lnum" href="#75" name="75">0075</a>&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;&#0160;<span class="keyword">return</span>&#0160;<span class="name">areas</span><br />
<a class="lnum" href="#76" name="76">0076</a>&#0160;&#0160;&#0160;&#0160;<span class="name">_expand_area</span>&#0160;<span class="op">=</span>&#0160;<span class="name">staticmethod</span><span class="op">(</span><span class="name">_expand_area</span><span class="op">)</span></code></div></body></html>